package refactor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import common.bean.DeviceBean;
import common.bean.FacDevInfo;
import common.standard.table.FacilityDeviceFieldEnum;
import common.standard.table.PageEnum;
import common.util.ConditionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import refactor.entity.FacilityDevice;
import refactor.mapper.FacilityDeviceMapper;
import refactor.service.IFacilityDeviceService;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author 杨健
 * @since 2019-08-19
 */
@Service
public class FacilityDeviceServiceImpl extends ServiceImpl<FacilityDeviceMapper, FacilityDevice> implements IFacilityDeviceService {
    private FacilityDeviceMapper facilityDeviceMapper;

    @Autowired
    public void setFacilityDeviceMapper(FacilityDeviceMapper facilityDeviceMapper) {
        this.facilityDeviceMapper = facilityDeviceMapper;
    }

    /**
     * 分页显示设备绑定情况
     *
     * @param info     条件参数
     * @param regionId 区域id
     * @param pageNum  当前页码
     * @return 返回分页包装对象
     */
    @Override
    public IPage<FacilityDevice> pageList(FacDevInfo info, Integer regionId, Integer pageNum) {
        QueryWrapper<FacilityDevice> facDevQueryWrapper = new QueryWrapper<>();

        facDevQueryWrapper.eq(FacilityDeviceFieldEnum.REGIONID.getFieldName(), regionId);

        // 设备条件
        DeviceBean deviceBean = new DeviceBean(info.getJg(), info.getTdd(), info.getJfs(), info.getJgs(),info.getMenjinhezi(),info.getDianbiao(),info.getShuibiao(),info.getYangan());
        ConditionUtil.addDeviceCondition(facDevQueryWrapper, deviceBean);

        // 搜索条件
        if (isNotBlank(info.getCondition())) {
            facDevQueryWrapper.nested(i -> i.like(FacilityDeviceFieldEnum.FACDES.getFieldName(), info.getCondition())
                    .or()
                    .like(FacilityDeviceFieldEnum.DEVCODE.getFieldName(), info.getCondition()));
        }

        // 安装条件
        if (isNotBlank(info.getJj())) {
            facDevQueryWrapper.eq(FacilityDeviceFieldEnum.DEVCODE.getFieldName(), "");
        }

        Page<FacilityDevice> facdevPage = new Page<>(pageNum, PageEnum.PAGE_SIZE.getValue());

        return facilityDeviceMapper.selectPage(facdevPage, facDevQueryWrapper);
    }
}
